<?php
/*
 * copyright (c) Burdenyuk
 */

class mod_reviewsnew extends module {

    private $table_reviews = "mod_reviewsnew";
    private $countmessages = null;

    public function init(){

        if($this->api->checkPOST("countmessages") ){
            if (is_numeric($_POST["countmessages"])) {

                $res=$this->api->dbo->select("systemoptions","*","WHERE keyname = 'reviewsnew'",_RESULT_BOOL);

                if ($res==true) {
                    $cm=$_POST["countmessages"];
                    $this->api->dbo->update("systemoptions","SET value = '$cm' WHERE keyname = 'reviewsnew'");
                } else {
                    $this->api->options->Set("reviewsnew",$_POST["countmessages"],"countmessages");
                }
            }
        }

        if ($this->api->options->get("reviewsnew","countmessages")) {
            $this->countmessages=$this->api->options->get("reviewsnew","countmessages");
        } else {
            $this->countmessages=10;
        } 
    }

    public function viewmessages(){
        
        if(isset($_GET["addmsg"])){
            print $this->api->message($this->lang->get("m0"));
        }

        $n = $this->api->dbo->select($this->table_reviews,"COUNT(*) as count",null,_RESULT_ROW);
        
        $n=$n["count"]; 

        $pages = ceil($n/$this->countmessages);

        if ( isset($_GET["page"]) and is_numeric($_GET["page"])) { $page=$_GET["page"]; }//fixme
        else { $page=0; }

        $pos = $page*$this->countmessages;

        $result = $this->api->dbo->select($this->table_reviews,
                "author, message, date"," order by id desc limit ".$this->countmessages." offset ".$pos); 

        $next=null; $previous=null;

        if ($pages>1) {
        if ($page==0) {
            $next=1; $previous=0;
        } elseif ($page>=1) {
            if (($pages-1)==$page) {
                $next=0; $previous=1;
            } else {
                $next=1; $previous=1;
            }
        }
        } else {
            $next=0; $previous=0;
        }

        $actionnext=null; $actionprevious=null;

        $actionnext = $this->api->base->GetActionLink("reviewsnew-viewmessages","page=".($page+1)); //"reviews-get","page=".($page-1)
        $actionprevious = $this->api->base->GetActionLink("reviewsnew-viewmessages","page=".($page-1));

        return $this->api->view->template("view",array(
            "var"=>&$result,
            "next"=>$next,
            "previous"=>$previous,
            "actionnext"=>$actionnext,
            "actionprevious"=>$actionprevious));
        //$this->insertmessage();
    }

    public  function insertmessage(){
        if($this->api->checkPOST("author","email","message","submit") ){
            list ($author, $email, $message) = $this->api->escapePOST("author", "email", "message");
            if (strlen($author)<2) {
                //print $this->api->message($this->lang->get(""));
                print $this->api->message($this->lang->get("e0"));
            }
            else if (!preg_match("/^[^@]+@[^@]+\.[^@]+$/i",$email,$trash)){
                print $this->api->message($this->lang->get("e1"));
            }
            else if ($message==""){ print $this->api->message($this->lang->get("e2")); }

            /*else if( $this->api->session->exists("ReviewAdded")  ){
                print $this->api->message($this->lang->get("e3"));
            }*/
        
            else {
                $this->api->dbo->insert($this->table_reviews, array(
                    "author"=>$author,
                    "email"=>$email,
                    "message"=>$message,
                    "date"=>date("Y-m-d H:i:s")));
            
            /*
            if ( isset($_GET["page"]) and is_numeric($_GET["page"])) { $page=$_GET["page"]; }//fixme
                else { $page=0; }
            * даний блок використовувати, якщо вивід відгуків по зростанню дати та
             *  $this->api->base->sendHeader("reviewsnew-viewmessages","page=".$page);
             * по спаданню, використовується page=0,
             * щоб після публікації відгука перенаправляло на першу сторінку виводу відгуків
            */

                $this->api->session->set("ReviewAdded",true);
                $this->api->base->sendHeader("reviewsnew-viewmessages","page=0&addmsg");
                //print $this->api->message("Повідомлення додано!");
            }
        }
        
        //return $this->api->view->template("newmessage");
    }

    public function adminmessages() {

        $n = $this->api->dbo->select($this->table_reviews,"COUNT(*) as count",null,_RESULT_ROW);

        $n=$n["count"];

        $pages = ceil($n/$this->countmessages);

        if ( isset($_GET["page"]) and is_numeric($_GET["page"])) { $page=$_GET["page"]; }//fixme
        else { $page=0; }

        $pos = $page*$this->countmessages;

        //$delvar - delete variable
        if(isset($_GET["delvar"]) and is_numeric($_GET["delvar"]) ){

            $delvar = $this->api->escapeString($_GET["delvar"]);

            $this->api->dbo->delete($this->table_reviews,"where id='".$delvar."' ");
        }


        $result = $this->api->dbo->select($this->table_reviews,
                "id, author, message, date, email"," order by id limit ".$this->countmessages." offset ".$pos);

        $next=null; $previous=null;

        if ($pages>1) {
        if ($page==0) {
            $next=1; $previous=0;
        } elseif ($page>=1) {
            if (($pages-1)==$page) {
                $next=0; $previous=1;
            } else {
                $next=1; $previous=1;
            }
        }
        } else {
            $next=0; $previous=0;
        }

        $actionnext=null; $actionprevious=null;

        $actionnext = $this->api->base->GetActionLink("reviewsnew-adminmessages","page=".($page+1)); 
        $actionprevious = $this->api->base->GetActionLink("reviewsnew-adminmessages","page=".($page-1));

        return $this->api->view->template("adminview",array(
            "var"=>&$result,
            "next"=>$next,
            "previous"=>$previous,
            "actionnext"=>$actionnext,
            "actionprevious"=>$actionprevious,
            "page"=>$page,
            "cm"=>$this->countmessages));
        
    }

    public function editmessage(){

        if($this->api->checkPOST("author","email","message","submit") ){
            list ($author, $email, $message) = $this->api->escapePOST("author", "email", "message");
            if (strlen($author)<2) {
                print $this->api->message($this->lang->get("e0"));
            }
            else if (!preg_match("/^[^@]+@[^@]+\.[^@]+$/i",$email,$trash)){
                print $this->api->message($this->lang->get("e1"));
            }
            else if ($message==""){ print $this->api->message($this->lang->get("e2")); }

            else {
                $id=$_GET["editvar"];
                if ($this->api->dbo->update($this->table_reviews,"set author='$author',email='$email',message='$message' where id='$id'")) {
                    print $this->api->message($this->lang->get("m1"));
                }
            }
        }

        if (isset($_GET["editvar"]) and is_numeric($_GET["editvar"]) ) {
            $res=$this->api->dbo->select($this->table_reviews,"*","where id='".$_GET["editvar"]."'",_RESULT_ROW);
            return $this->api->view->template("edit",array("var"=>&$res));
        }
    }
//"author, message, date"," order by id desc limit ".$this->countmessages." offset ".$pos);

    public function deleteallmsg(){
        if ($this->api->dbo->delete($this->table_reviews,null)) {
            print $this->api->message($this->lang->get("m2"));
        }
    }


    public function install() {
        $table = new sqltable($this->table_reviews);
        $table->addfield("id", "int_auto");
        $table->addkey("primary", "id");
        $table->addfield("message", "text");
        $table->addfield("author", "char");
        $table->addfield("date", "datetime");
        $table->addfield("email", "char");
        $this->api->dbo->createtable($table);
    }

    public function  uninstall() {

    }

}


?>